package org.penguin.study.bk.web.servlet;

import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@Slf4j
@WebServlet(name = "hiServlet", value = "/hiServlet")
public class HiServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doBusiness(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doBusiness(request, response);
    }

    protected void doBusiness(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        log.info("在spring中的servlet");
       HttpSession session = request.getSession(true);
        session.setAttribute("name", "无名");
        response.setContentType("text/html;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        out.println("嵌套在Spring Boot中的servlet");
        session.setAttribute("name", "无名");
        session.removeAttribute("name");
        out.flush();
        out.close();
    }
}
